<?php

/**
 * @author yupoxiong<i@yufuping.com>
 */

namespace generate\field;

use generate\traits\Tools;

class Field
{
    use Tools;


    public static $required = '';
    public static $verify = '';

    //当前字段可用规则
    public static $rules = [
        //非空
        'required'   => '非空',
        'wechat'     => '微信号',
        'qq'         => 'QQ号',
        'account'    => '账号',
        'cn_name'    => '中文姓名',
        'car_number' => '车牌号',
        //自定义正则
        'regular'    => '自定义正则'
    ];


    //列表名字
    public static $listNameHtml = <<<EOF
<th>[FORM_NAME]</th>\n
EOF;

    //列表字段
    public static $listFieldHtml = <<<EOF
<td>{\$item.[FIELD_NAME]}</td>\n
EOF;

    //列表关联筛选
    public static $listSearchRelationHtml = <<<EOF
<div class="layui-inline">
    <select name="[FIELD_NAME]" id="[FIELD_NAME]" >
        <option value="">[FORM_NAME]</option>
        {foreach name='[FIELD_NAME1]_list' id='item'}
        <option value="{\$item.id}" {if isset($[FIELD_NAME]) && ''!==$[FIELD_NAME] && $[FIELD_NAME]==\$item.id}selected{/if}>{\$item.[RELATION_SHOW]}</option>
        {/foreach}
    </select>
</div>
\n
EOF;

    //列表自定义筛选数据
    public static $listSearchSelectHtml = <<<EOF
<div class="layui-inline">
    <select name="[FIELD_NAME]" id="[FIELD_NAME]" >
        <option value="">[FORM_NAME]</option>
        [SELECT_OPTION]
    </select>
</div>
\n
EOF;

    //列表日期筛选
    public static $listSearchDate = <<<EOF
<div class="layui-inline">
    <input value="{\$[FIELD_NAME]|default=''}" readonly name="[FIELD_NAME]" id="[FIELD_NAME]"  placeholder="[FORM_NAME]">
</div>
<script>
    laydate.render({
        elem: '#[FIELD_NAME]',
        range: true,
    });
</script>\n
EOF;

    //列表日期时间筛选
    public static $listSearchDatatime = <<<EOF
<div class="layui-inline">
    <input value="{\$[FIELD_NAME]|default=''}" readonly name="[FIELD_NAME]" id="[FIELD_NAME]" class="layui-input input-sm indexSearchDatetimeRange" placeholder="[FORM_NAME]">
</div>
<script>
    laydate.render({
        elem: '#[FIELD_NAME]',
        type: 'datetime',
        range: true,
    });
</script>\n
EOF;



    //图片字段显示
    public static $listImgHtml = <<<EOF
<td>  <img class="photosimg" src="{\$item.[FIELD_NAME]}"></td>\n
EOF;

    public static $listColorHtml = <<<EOF
<td> <div class="color_list" style="background-color: {\$item.[FIELD_NAME]};"></div></td>
\n
EOF;

    public static $listMultiImgHtml = <<<EOF
<td class="dataListMultiImg" >
{foreach name='item.[FIELD_NAME]' id='item_[FIELD_NAME]'}
<img class="dataListImg "  src="{\$item_[FIELD_NAME]}">
{/foreach}
</td>
\n
EOF;

    public static $listMultiFileHtml = <<<EOF
<td class="dataListMultiFile">
{foreach name='item.[FIELD_NAME]' id='item_[FIELD_NAME]'}
<a target="_blank" href="{\$item_[FIELD_NAME]}">查看文件</a>
{/foreach}
</td>
EOF;
    public static $listKeyWordsHtml = <<<EOF
<td> {\$item.[FIELD_NAME]_text} </td>
EOF;
    //status字段获取器为switch的时候自动显示为field_name_text
    public static $listSwitchHtml = <<<EOF
<td>{\$item.[FIELD_NAME]_text}</td>\n
EOF;

 
    //验证器场景
    public static  $validateSceneCode =
        <<<EOF
        'admin_add'     => [[RULE_FIELD]],
        'admin_edit'    => ['id', [RULE_FIELD]],
        'admin_del'     => ['id', ],
        'admin_disable' => ['id', ],
        'admin_enable'  => ['id', ],
        'api_add'       => [[RULE_FIELD]],
        'api_info'      => ['id', ],
        'api_edit'      => ['id', [RULE_FIELD]],
        'api_del'       => ['id', ],
        'api_disable'   => ['id', ],
        'api_enable'    => ['id', ],
EOF;

    //获取选择的字段和字段对应的验证optionDOM代码
    public function getFormSelectOption($field_type)
    {
        $result     = ['text', ''];
        $field_info = $this->getFieldInfo($file_name = '', $field_type);
        switch ($field_info['type']) {
            case 'tinyint':
                $result[0] = 'switch';
                break;
            case 'smallint' || 'mediumint' || 'int' || 'bigint' || 'float' || 'double' || 'decimal':
                $result[0] = 'number';
                break;
            case 'char' || 'varchar':
                $result[0] = 'text';
                break;
            case 'tinytext' || 'tinyblob' || 'text' || 'blob' || 'longtext' || 'longblob':
                $result[0] = 'editor';
                break;
            case 'date':
                $result[0] = 'date';
                break;
            case 'datetime' || 'timestamp':
                $result[0] = 'datetime';
                break;
            case 'time':
                $result[0] = 'time';
                break;
            case 'year':
                $result[0] = 'year';
                break;
            default:
                $result[0] = 'text';
                break;
        }


        $result[1] = $this->getValidateOption($result[0], $field_info['length']);
        return $result;
    }

    //根据表单类型和长度返回相应的验证
    public function getValidateOption($type, $length = '')
    {
        $fieldName = parse_name($type, 1, true);
        $data = $fieldName::rule($length);
        return $data;
    }
    //规则
    public static function rule( $length = '')
    {

        $rule = [];
        $rules = static::$rules;
        foreach ($rules as $key => $value) {
            $key  = str_replace('[LENGTH]', $length, $key);
            if ($key == 'required') {
                $rule[] = [
                    'name' => $value,
                    'value' => $key,
                    'selected' => true
                ];
            } else {
                $rule[] = [
                    'name' => $value,
                    'value' => $key
                ];
            }
        }
        return $rule;
    }


    public static function createHtmlRule($rule)
    {
        $html = '';
        if (key_exists($rule, self::$rules)) {
            $html .= '';
        }
    }
    public static function getValidateHtml($data)
    {
        if (isset($data)) {
            $data = explode(',', $data);
            $dataCount = count($data) - 1;
            foreach ($data as $key => $vo) {
                switch ($vo) {
                    case 'required':
                        self::$required = 'layui-required';
                        self::$verify .= 'required';
                        break;
                    case 'id_card':
                        self::$verify .= 'identity';
                        break;
                    case 'email':
                        self::$verify .= 'email';
                        break;
                    case 'mobile':
                        self::$verify .= 'phone';
                        break;
                    case 'url':
                        self::$verify .= 'url';
                        break;
                    case 'ip':
                        self::$verify .= 'ip';
                        break;
                }
                if ($dataCount != $key) {
                    self::$verify .= '|';
                }
            }
        }
    }
}
